package code.net
{
	import code.app.AppDmList;
	import code.main.modle.table.Player;
	
	import com.yy.net.ProcessCmd;
	import com.yy.net.ProcessNetCmd;
	import com.yy.net.socketclass.MySocket;

	/**
	 * 管理器
	 * 单例模式
	 */
	public class SocketManager
	{
		
		private static var _instance:SocketManager;
		private var _socket:MySocket = null;
		private var _clentSocket:ProcessNetCmd = null;
		private var _gameProcess:ProcessCmd = null;
		private var _player:Player;
		public function SocketManager(xx:XX)
		{
			if(!xx) {
				throw new Error("不能实例化");
			}
			_clentSocket = new ProcessNetCmd(_gameProcess);
			_socket = _clentSocket.getMySocket();
			_player = AppDmList.getInstance().player;
		}
		
		public static function getInstance() : SocketManager
		{
			if(!_instance) {
				_instance = new SocketManager(new XX());
			}
			return _instance;
		}
		
		private function init():void
		{
			trace("SocketManager.init");
		}
		
		/**
		 *注册回调方法 
		 * @param key
		 * @param func
		 * 
		 */
		public function register(key:int, func:Function):void {
			
			this._clentSocket.register(key, func);
		}
		
		/**
		 * 连接
		 */
		public function connect(host:String, port:int):void
		{
			if (!_socket)
			{
				_clentSocket = new ProcessNetCmd(_gameProcess);
				_socket = _clentSocket.getMySocket();
			}
			_socket.connect(host, port);
		}
		
		/*
		* 连接关闭
		* 
		*/
		public function close():void
		{
			_socket.close();
		}
		
		/**
		 *返回socket 
		 */ 
		public function get socket():MySocket
		{
			return _socket;
		}
		
		/**
		 * 是否连接
		 */
		public function get connected():Boolean 
		{
			return _socket.getConnecState();
		}
//#####################################斗牛逻辑命令##################################	
		
		/**
		 * 发送登陆包
		 */
		public function login():void
		{
			_socket.writeBegin(CommandTyte.SEND_LOGIN);
			
			_socket.writeInt(int(_player.gameId));
			_socket.writeString(String(_player.userName));
			_socket.writeByte(_player.level);
			_socket.writeByte(_player.titleId);
			_socket.writeByte(_player.gender);
			_socket.writeByte(_player.vipLevel);
			_socket.writeString(String(_player.pic));
			_socket.writeString(_player.userKey);
			_socket.writeInt(_player.giftId);
			_socket.writeInt(_player.tableVo.tableId);
			
			_socket.writeEnd();
			_socket.sendcmd();
			trace("发送登陆命令" + CommandTyte.SEND_LOGIN.toString(16) + ":gameId=>" + _player.gameId + ":tableId->" + _player.tableVo.tableId);
		}
		
		/**
		 *玩家坐下命令 
		 * @param setId
		 * 
		 */
		public function seat(money:int, setId:int):void {
			_socket.writeBegin(CommandTyte.SEND_SEAT);
			
			_socket.writeInt(int(_player.gameId));
			_socket.writeInt(_player.tableVo.tableId);
			_socket.writeInt(money);
			_socket.writeByte(setId);
			
			_socket.writeEnd();
			_socket.sendcmd();
			trace("玩家坐下命令 " + CommandTyte.SEND_SEAT.toString(16) + ":gameId=>" + _player.gameId + ":tableId->" + _player.tableVo.tableId);
		}
		
		/**
		 *抢庄 
		 * @param seatId
		 * @param boo
		 * 
		 */
		public function grabBank(seatId:int, boo:Boolean):void {
			_socket.writeBegin(CommandTyte.SEND_GRAB_BANKER);
			
			_socket.writeByte(seatId);
			_socket.writeByte(boo?1:0);
			
			_socket.writeEnd();
			_socket.sendcmd();
		}
		
		/**
		 *下注 
		 * @param gameId
		 * @param seatId
		 * @param money
		 * 
		 */
		public function bet(gameId:int, seatId:int, money:int):void {
			trace("==============>发送下注数据");
			trace("money:" + money);
			_socket.writeBegin(CommandTyte.SEND_BET);
			
			_socket.writeInt(gameId);
			_socket.writeByte(seatId);
			_socket.writeInt(money);
			
			_socket.writeEnd();
			_socket.sendcmd();
			trace("<==============发送完毕");
		}
		
		/**
		 * 发送选牌数据
		 * 
		 */
		public function showCard(gameId:int, seatId:int, cards:Array, cowNumb:int=10):void {
			trace("==============>发送选牌数据");
			_socket.writeBegin(CommandTyte.SEND_SELECT_CARD);
			
			_socket.writeInt(gameId);
			_socket.writeByte(seatId);
			var i:int;
			for(i=0; i<5; i++) {
				_socket.writeShort(cards[i]);
				trace(int(cards[i]).toString(16));
			}
			trace("sowNumb:" + cowNumb);
			_socket.writeByte(cowNumb);
			_socket.writeEnd();
			_socket.sendcmd();
			trace("<==============发送完毕");
		}
		
		
		/**
		 *发送结算响应
		 * 
		 */
		public function settlementOk():void {
			trace("==============>发送结算完毕响应");
			_socket.writeBegin(CommandTyte.SEND_SETTLEMENT_OK);
			_socket.writeInt(_player.seatId);
			_socket.writeEnd();
			_socket.sendcmd();
			trace("<==============发送完毕");
		}
		
		/**
		 *重连 
		 * 
		 */
		public function reconnecct():void {
			trace("==============>发送结算完毕响应");
			_socket.writeBegin(CommandTyte.SEND_RECONNECT);
			_socket.writeInt(int(_player.gameId));
			_socket.writeInt(_player.tableVo.tableId);
			_socket.writeEnd();
			_socket.sendcmd();
			trace("<==============发送完毕");
		}
		
		/**
		 *站起 
		 * 
		 */
		public function upSeat():void {
			trace("==============>发送站起");
			_socket.writeBegin(CommandTyte.SEND_PLAYER_UP);
			_socket.writeByte(_player.seatId);
			_socket.writeEnd();
			_socket.sendcmd();
			trace("<==============发送完毕");
		}
		/**
		 *返回大厅 
		 * 
		 */
		public function goHall():void {
			trace("==============>发送返回大厅");
			_socket.writeBegin(CommandTyte.SEND_PLAYER_QUIT);
			_socket.writeInt(int(_player.gameId));
			_socket.writeEnd();
			_socket.sendcmd();
			trace("<==============发送完毕");
		}
		
		/**
		 *发送聊天内容 
		 * 
		 */
		public function sendChat(msg:String=""):void {
			trace("==============>发送聊天内容");
			_socket.writeBegin(CommandTyte.SEND_CHAT);
			_socket.writeString(_player.userName);
			_socket.writeString(msg);
			_socket.writeEnd();
			_socket.sendcmd();
			trace("<==============发送完毕");
		}
		
		/**
		 *发送表情 
		 * @param id 表情id
		 * 
		 */
		public function sendExpression(id:int):void {
			trace("==============>发送表情");
			_socket.writeBegin(CommandTyte.SEND_EMOTION);
			_socket.writeInt(int(_player.gameId));
			_socket.writeByte(id);
			_socket.writeEnd();
			_socket.sendcmd();
			trace("<==============发送完毕");
		}
	}
}
class XX{}